#include "dboperator.h"

DbOperator::DbOperator()
{
    // 建立过一次连接后会使用默认连接方式
    if (QSqlDatabase::contains("qt_sql_default_connection"))
    {
        db = QSqlDatabase::database("qt_sql_default_connection");
        qDebug() << "数据库连接建立成功";
    }
    else
    {
        //建立和sqlite数据的连接
        db = QSqlDatabase::addDatabase("QSQLITE");
        QString dbname = QDir::currentPath() +QString("/")+ QString("pokemon-data.db");
        db.setDatabaseName(dbname);
        qDebug() << "数据库连接建立成功";
    }

}

DbOperator::~DbOperator()
{
    db.close();
    qDebug() << "断开数据库连接";
}

bool DbOperator::OpenDb()
{
    if(db.open()) {
        qDebug() << "数据库连接成功";
        return true;
    }
    else  {
        qDebug() << "数据库连接失败";
        return false;
    }

}

bool DbOperator::TableExist(QString &tableName)
{
    if(db.tables().contains(tableName)) {
        return true;
    }
    return false;
}

void DbOperator::insertOneMsg(container &msg)
{
    QSqlQuery query(db);
    query.prepare("INSERT INTO helloworld (username, password) "
                  "VALUES (:username, :password)");
    query.bindValue(":username", msg.username);
    query.bindValue(":password", msg.password);
    bool success = query.exec();
    if(!success) {
        QSqlError e = query.lastError();
        qDebug() << e.driverText() << QString("failed");
    }
}

bool DbOperator::buildTable()
{
    QSqlQuery query(db);      // 建立连接
    bool success = query.exec("create table helloworld "
                              "(username VARCHAR(50), "
                              "password VARCHAR(50))");
    if (success) {
        qDebug() << QObject::tr("创建数据库表成功");
        return true;
    }
    else {
        qDebug() << QObject::tr("创建数据库表失败");
        return false;
    }
}

container DbOperator::searchTrainerMsg(int id)
{
    container trainer;
    QSqlQuery query(db);
//    query.prepare("select * from trainer where username='xgc'");   // 记得字串要加‘’
    QString cmd = QString("select * from trainer where id=%1").arg(id);
    query.prepare(cmd);   // 记得字串要加‘’
    query.exec();
    if (query.next()) {
//        qDebug() << query.value("password").toString();    // 这里可以填索引，也可以填表头
//        qDebug() << query.value("username").toString();    // 这里可以填索引，也可以填表头
        trainer.id = query.value("id").toInt();
        trainer.username = query.value("username").toString();
        trainer.password = query.value("password").toString();
        trainer.sex = query.value("sex").toString();
        trainer.honor = query.value("honor").toInt();
        trainer.goldcoin = query.value("goldcoin").toInt();
        trainer.knowledge = query.value("knowledge").toInt();
        trainer.idno = query.value("idno").toString();;
        trainer.curclothes = query.value("curclothes").toString();;
        trainer.clothes = query.value("clothes").toString();
        trainer.item = query.value("item").toString();
    }
    else {
        qDebug() << "没有搜索到结果";
    }
    return trainer;
}

pokemon DbOperator::searchPokemonMsg(int idno)
{
    pokemon p;
    QSqlQuery query(db);
//    query.prepare("select * from trainer where username='xgc'");   // 记得字串要加‘’
    QString cmd = QString("select * from pokemon where idno=%1").arg(idno);
    query.prepare(cmd);   // 记得字串要加‘’
    query.exec();
    if (query.next()) {
        p.id = query.value("id").toInt();
        p.idno = query.value("idno").toInt();       // 决定宠物的唯一性
        p.attack = query.value("attack").toInt();
        p.defense = query.value("defense").toInt();
        p.maxhp = query.value("maxhp").toInt();
        p.curhp = query.value("curhp").toInt();
        p.attribute = query.value("attribute").toString();
        p.fetter = query.value("fetter").toInt();
        p.lv = query.value("lv").toInt();
        p.skill = query.value("skill").toString();
    }
    else {
        qDebug() << "没有搜索到结果";
    }
    return p;

}

void DbOperator::updateGoldCoin(int id, int newCoinNum)
{
    QSqlQuery query(db);
//    QString psw("654321");
    QString cmd;
//    cmd = QString("update helloworld set password='%1' where username='xgc'").arg(psw);
    cmd = QString("update trainer set goldcoin='%1' where id='%2'").arg(newCoinNum).arg(id);
    query.prepare(cmd);
    bool success = query.exec();
    if (success) {
        qDebug() << "修改数据成功";
    }
    else {
        QSqlError e = query.lastError();
        qDebug() << "修改数据失败";
        qDebug() << e.driverText();
    }
}

void DbOperator::updateItem(int id, QString newItem)
{
    QSqlQuery query(db);
    QString cmd;
    cmd = QString("update trainer set item='%1' where id='%2'").arg(newItem).arg(id);
    query.prepare(cmd);
    bool success = query.exec();
    if (success) {
        qDebug() << "修改数据成功";
    }
    else {
        QSqlError e = query.lastError();
        qDebug() << "修改数据失败";
        qDebug() << e.driverText();
    }
}

void DbOperator::updatefetter(int idno, int fetter)
{
    QSqlQuery query(db);
    QString cmd;
    cmd = QString("update pokemon set fetter='%1' where idno='%2'").arg(fetter).arg(idno);
    query.prepare(cmd);
    bool success = query.exec();
    if (success) {
        qDebug() << "修改数据成功";
    }
    else {
        QSqlError e = query.lastError();
        qDebug() << "修改数据失败";
        qDebug() << e.driverText();
    }

}

void DbOperator::cureMaxHP(int idno)
{
    QSqlQuery query(db);
    QString cmd;
    pokemon temp = searchPokemonMsg(idno);
    cmd = QString("update pokemon set curhp='%1' where idno='%2'").arg(temp.maxhp).arg(idno);
    query.prepare(cmd);
    bool success = query.exec();
    if (success) {
        qDebug() << "修改数据成功";
    }
    else {
        QSqlError e = query.lastError();
        qDebug() << "修改数据失败";
        qDebug() << e.driverText();
    }
}

void DbOperator::setcurHp(int idno, int curHp1)
{
    QSqlQuery query(db);
    QString cmd;
    pokemon temp = searchPokemonMsg(idno);
    cmd = QString("update pokemon set curhp='%1' where idno='%2'").arg(curHp1).arg(idno);
    query.prepare(cmd);
    bool success = query.exec();
    if (success) {
        qDebug() << "修改数据成功";
    }
    else {
        QSqlError e = query.lastError();
        qDebug() << "修改数据失败";
        qDebug() << e.driverText();
    }

}

QSqlDatabase DbOperator::getDb() const
{
    return db;
}

void DbOperator::setNullPokemon(pokemon &p)
{
    p.id = 0;
    p.idno = 0;       // 决定宠物的唯一性
    p.attack = 0;
    p.defense = 0;
    p.maxhp = 0;
    p.curhp = 0;
    p.attribute = "";
    p.fetter = 0;
    p.lv = 0;
    p.skill = "";
}

